﻿using ORM.Base;
using ORM.DB;
using ORM.Util;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ORM.Applicaiton
{
    public class DBRow
    {
        public Record record;
        private SQLBuilder sqlbuilder = new SQLBuilder();
        private bool _TraceSQL;
        public bool TraceSQL
        {
            set
            {
                if (record!=null)
                {
                    record.TraceSQL = value;
                }
                else
                {
                    _TraceSQL = value;
                }
                

            }
            get
            {
                if (record != null)
                {
                    return record.TraceSQL;
                }
                else
                {
                    return _TraceSQL;
                }
            }
        }
        public DbTransaction Transition
        {
            set
            {
                record.Transition = value;
              
            }
            get
            {
                return record.Transition;
            }
        }
       
        public object this[string ColumnName]
        {
            set
            {
                record[ColumnName] = value;
            }
            get
            {
                return record[ColumnName];
            }
        }

        public DBRow(string TableName)
        {
            record = DBFactory.CreateRecord(TraceSQL);
            record.TableName = TableName;
            record.Init();
        }

        public DBRow(string TableName, string ConnectStringName)
        {

            record = DBFactory.CreateRecord(TraceSQL);
            record.TableName = TableName;
            record.ConnectStringName = ConnectStringName;
            record.Init();
         

        }
        public DBRow(string TableName, string KeyFieldName, string KeyFieldValue, string ConnectStringName)
        {
            record = DBFactory.CreateRecord(TraceSQL);
            record.TableName = TableName;
            record.KeyFieldName = KeyFieldName;
            record.KeyFieldValue = KeyFieldValue;
            record.ConnectStringName = ConnectStringName;
            record.Init();
            record.SQL = sqlbuilder.BuildRecordSQL(record, Enum.SQLType.FINDONE);
            record.Query();
           

        }

        public DBRow(string TableName, string KeyFieldName, string KeyFieldValue)
        {
            record = DBFactory.CreateRecord(TraceSQL);
            record.TableName = TableName;
            record.KeyFieldName = KeyFieldName;
            record.KeyFieldValue = KeyFieldValue;
            record.Init();
            record.SQL = sqlbuilder.BuildRecordSQL(record, Enum.SQLType.FINDONE);
            record.Query();
        }

        public bool IsExist()
        {
            return record.IsExist;
        }

        public void Insert()
        {
            record.SQL = record.SQL = sqlbuilder.BuildRecordSQL(record, Enum.SQLType.INSERT);
            record.Execute();
            record.ClearParams();
        }

        public void Delete()
        {
            record.SQL = record.SQL = sqlbuilder.BuildRecordSQL(record, Enum.SQLType.REMOVE);
            record.Execute();
            record.ClearParams();
        }

        public void Update()
        {
            record.SQL = record.SQL = sqlbuilder.BuildRecordSQL(record, Enum.SQLType.SAVE);
            record.Execute();
            record.ClearParams();
        }

        public static bool ExecuteTransition_SDB(Action<DbTransaction> TransitionAction, out string ExceptionStr)
        {
            bool IsSuccess = true;
            ExceptionStr = string.Empty;
            DBHelper _DBHelper = new DBHelper();
            using (DbConnection connection = _DBHelper.GetConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();
                try
                {
                    TransitionAction(transaction);
                    transaction.Commit();
                }
                catch (System.Exception e)
                {
                    IsSuccess = false;
                    ExceptionStr = e.Message + e.StackTrace;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                    transaction = null;
                }
            }
            return IsSuccess;

        }

        public static bool ExecuteTransition_SDB(Action<DbTransaction> TransitionAction, string ConnectStringName, out string ExceptionStr)
        {
            bool IsSuccess = true;
            ExceptionStr = string.Empty;
            DBHelper _DBHelper = new DBHelper(ConnectStringName);
            using (DbConnection connection = _DBHelper.GetConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();
                try
                {
                    TransitionAction(transaction);
                    transaction.Commit();
                }
                catch (System.Exception e)
                {
                    IsSuccess = false;
                    ExceptionStr = e.Message + e.StackTrace;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                    transaction = null;
                }
            }
            return IsSuccess;

        }


    }
}
